// pages/perCenter/perCenter.js
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      nickName: "用户名",
      avatarUrl: "/images/user.png"
    },
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    role: '用户',
    enrollList: 0, 
    participateList: 0, 
    sponsorList: 0 
  },

  onTapEnrollList() {
    wx.navigateTo({
      url: '/pages/perCenter/voteList?voteType=enroll',
    })
  },
  onTapSponsorList() {
    wx.navigateTo({
      url: '/pages/perCenter/voteList?voteType=participate',
    })
  },
  onTapVoteList() {
    wx.navigateTo({
      url: '/pages/perCenter/voteList?voteType=sponsor',
    })
  },

  /**
   * 进入二级页面
   */
  onTapFeedback() {
    wx.navigateTo({
      url: '/pages/feedback/feedback',
    })
  },
  getMyVoteListLengthFromServer() {
    wx.cloud.callFunction({
      name: 'voteList',
      data: {
        voteType: 'enroll'
      }
    }).then(res => {
      this.setData({
        enrollList: res.result.data.length
      })
    })
    wx.cloud.callFunction({
      name: 'voteList',
      data: {
        voteType: 'participate'
      }
    }).then(res => {
      this.setData({
        participateList: res.result.data.length
      })
    })
    wx.cloud.callFunction({
      name: 'voteList',
      data: {
        voteType: 'sponsor'
      }
    }).then(res => {
      this.setData({
        sponsorList: res.result.data.length
      })
    })
  },
  //判断用户属性
  checkRole() {
    wx.cloud.callFunction({
      name: 'userInfo',
      data: {
        roleid: 'oXCIO5OsV3fsnmjxs7EYdrlkXL5A'
      }
    }).then(res => {
      //console.log(res.result.role)
      this.setData({
        role: res.result.role
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      this.getMyVoteListLengthFromServer()
      this.checkRole()
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        this.getMyVoteListLengthFromServer()
        this.checkRole()
        console.log(this.data.userInfo)
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          this.getMyVoteListLengthFromServer()
          this.checkRole()
        }
      })
    }

  },

  //更新用户头像和昵称信息
  bindGetUserInfo(e) {
    if (this.data.hasUserInfo) {
      console.log(e)
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      this.getMyVoteListLengthFromServer()
      this.checkRole()
      wx.showToast({
        title: '更新信息成功',
        duration: 2000,
        icon: 'none',
        mask: true,
      })
    } else {
      console.log(e)
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      this.getMyVoteListLengthFromServer()
      this.checkRole()
      wx.showToast({
        title: '获取信息成功',
        duration: 2000,
        icon: 'none',
        mask: true,
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '分享小程序',
      path: '/pages/welcome/welcome'
    }
  }
})